<?php
/* ******************************
 *	letterA Service List aJax actions
 * ******************************
 */

include('../../../includes/classes/core.php');

$action		= $_REQUEST['act'];
$error		= '';
$data		= '';
$user_id	= $_SESSION['USERID'];
$count		= $_REQUEST['count'];
$hidden		= $_REQUEST['hidden'];
$local_id	= $_REQUEST['local_id'];
$warehouse_id	= $_REQUEST['warehouse_id'];
switch ($action) {
    case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);

        break;
    case 'get_edit_page':
	    $list_id	= $_REQUEST['id'];
		$page		= GetPage(GetServiceList($list_id));

		$data		= array('page'	=> $page);

        break;
    case 'get_list':


	    $rResult = mysql_query("SELECT warehouse_detail.id,
                                	   production.`name`,
                                	   COUNT(warehouse_detail.id)
                                FROM   warehouse_detail
                                LEFT JOIN production ON production.id = prod_id
                                WHERE warehouse_detail.warehouse_id='$warehouse_id' AND warehouse_detail.actived=1 AND warehouse_detail.type=1
                                GROUP BY prod_id ");

		$data = array(
			"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}

        break;
    case 'save_service':
		$service_id		= $_REQUEST['id'];
		$local_id		= $_REQUEST['lid'];
		$production_id	= GetProductID($_REQUEST['sn']);

		$arr = array(
				"warehouse_id"	=> $_REQUEST['wid'],
				"production_id"	=> $production_id,
				"quantity"		=> $_REQUEST['sq'],
				"comment"		=> htmlspecialchars($_REQUEST['sc'], ENT_QUOTES)
		);
		if($service_id == ''){
			AddServiceList($user_id, $local_id, $arr);
		}else{
		//	SaveServiceList($service_id, $user_id, $arr);
		//	$local_id=$service_id;
		}


;

        break;
    case 'disable':
		$list_id = $_REQUEST['id'];
		DisableServiceList($list_id);

        break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	letterA Service List Functions
 * ******************************
 */

function AddServiceList($user_id, $local_id, $arr)
{
$sql="INSERT INTO	`warehouse_detail`
						(`user_id`, `warehouse_id`,`prod_id`)
					VALUES ($user_id, $arr[warehouse_id],'$arr[production_id]')";
	for($i=1;  $i < $arr[quantity]; $i++){
						$sql.=",\n($user_id, $arr[warehouse_id],'$arr[production_id]')";
	}
	mysql_query($sql);
}

function SaveServiceList($service_id, $user_id, $arr)
{

}

function DisableServiceList($list_id)
{
    $aRow = mysql_fetch_assoc(mysql_query("SELECT	`warehouse_detail`.`warehouse_id` as `warehouse_id`,
                                            				`warehouse_detail`.`prod_id`
                                            FROM				`warehouse_detail`
                                            WHERE				`warehouse_detail`.`id` = '$list_id'"));
    mysql_query("UPDATE
				`warehouse_detail`
				SET
				`actived`	= 0
				WHERE
				warehouse_id='$aRow[warehouse_id]' AND prod_id='$aRow[prod_id]'");
}

function GetProductID($prod_name)
{
	$prod_name = htmlspecialchars($prod_name, ENT_QUOTES);
    $res = mysql_fetch_assoc(mysql_query("SELECT	`id`
										  FROM		`production`
										  WHERE		`name` = '$prod_name'"));
	return $res['id'];
}

function GetServiceList($list_id)
{
    $res = mysql_fetch_assoc(mysql_query("SELECT    `warehouse_detail`.`id`,
                                                    `production`.`name`,
                                                    COUNT(*) AS quantity,
                                            `warehouse_detail`.`comment`
                                            FROM	 `warehouse_detail`
                                            LEFT JOIN `production` ON		`warehouse_detail`.`prod_id` = `production`.`id`
                                            LEFT JOIN  warehouse_detail AS wd ON warehouse_detail.warehouse_id=wd.warehouse_id AND wd.prod_id=warehouse_detail.prod_id
                                            WHERE	`warehouse_detail`.`id` = '$list_id'"));
	return $res;
}

function GetPage($res = '')
{
	$data = '
	<div id="dialog-form">
	    <fieldset>
	    	<legend>ძირითადი ინფორმაცია</legend>
    		<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="service_name">დასახელება</label></td>
					<td>
						<div class="seoy-row" id="client_proruction_seoy">
							<input type="text" id="client_proruction" class="idle seoy-address" onblur="this.className=\'idle seoy-address\'" onfocus="this.className=\'activeField seoy-address\'" value="' . $res['name'] . '" />
							<button id="client_proruction_btn" class="combobox">client_proruction</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="service_quantity">რაოდენობა</label></td>
					<td>
						<input type="text" id="service_quantity" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['quantity'] . '" />
					</td>
				</tr>
				<tr class="comment">
					<td style="width: 170px;" valign="top"><label for="service_comment">შენიშვნა</label></td>
					<td>
						<textarea id="service_comment" class="idle large" cols="40" rows="3">' . $res['comment'] . '</textarea>
					</td>
				</tr>
			</table>
        </fieldset>
		<!-- ID -->
		<input type="hidden" id="service_id" value="' . $res['id'] . '" />
    </div>
    ';
	return $data;
}

?>